<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Tab选项卡</title>
  <style>
    .container {
      max-width: 800px;
      margin: 30px auto;
      border-radius: 4px;
      box-shadow: 0 0 4px 0px rgba(0, 0, 0, 0.2);
      padding: 16px;
    }

    .tabs .tab-header {
      position: relative;
      display: flex;
      font-size: 14px;
      color: #303030;
      line-height: 2;
      font-weight: 500;
      border-bottom: 1px solid #ddd;
    }

    .tabs .tab-item {
      margin-right: 30px;
      margin-bottom: -2px;
      cursor: pointer;
    }

    .tabs .line {
      position: absolute;
      top: 100%;
      transform: translateX(0);
      /* width: 0; */
      height: 2px;
      background-color: #409eff;
      transition: all .3s;
    }

    .tabs .tab-item.active {
      color: #409eff;
      /* border-bottom: 2px solid #409eff; */
    }

    .tabs .tab-panel {
      padding: 16px 0;
      font-size: 16px;
      display: none;
    }

    .tabs .tab-panel.active {
      display: block;
    }
  </style>
</head>

<body>
  <div class="container">
    <h2>Tab选项卡</h2>
    <div class="tabs">
      <div class="tab-header">
        <span class="tab-item active">用户管理</span>
        <span class="tab-item">配置管理</span>
        <span class="tab-item">角色管理</span>
        <span class="tab-item">定时任务补偿</span>
        <span class="line"></span>
      </div>
      <div class="tab-content">
        <div class="tab-panel active">用户管理</div>
        <div class="tab-panel">配置管理</div>
        <div class="tab-panel">角色管理</div>
        <div class="tab-panel">定时任务补偿</div>
      </div>
    </div>
  </div>

  <script>
    const $ = s => document.querySelector(s)
    const $$ = s => document.querySelectorAll(s)

    const $$tabItems = $$('.tabs .tab-item')
    const $$tabPanels = $$('.tabs .tab-panel')
    const $line = $('.tabs .line')
    $line.style.width = $$tabItems[0].offsetWidth + 'px'
    $$tabItems.forEach($tab => {
      $tab.onclick = function () {
        $$tabItems.forEach($tab => $tab.classList.remove('active'))
        this.classList.add('active')
        // offsetLeft表示相对于relative元素的左边的宽度，否则会相对于body
        console.log(this.offsetWidth)
        console.log(this.offsetLeft)
        $line.style.width = this.offsetWidth + 'px'
        // $line.style.left = this.offsetLeft + 'px'
        $line.style.transform = `translateX(${this.offsetLeft}px)`
        let index = [...$$tabItems].indexOf(this)
        $$tabPanels.forEach($tab => $tab.classList.remove('active'))
        $$tabPanels[index].classList.add('active')
      }
    })
  </script>
</body>

</html>